package tang.server;

import lombok.Data;
import tang.server.processor.FinalRequestProcessor;
import tang.server.processor.PreRequestProcessor;
import tang.server.processor.RequestProcessor;
import tang.server.processor.SyncRequestProcessor;

/**
 * @author Tang
 * @classname ZkServer
 * @description [  ]
 * @date 2021/2/28 21:27
 */
@Data
public class ZkServer {

    private ZkServerShutdownHandler zkServerShutdownHandler;

    private NioServerCnxnFactory nioServerCnxnFactory;

    private States states;

    private RequestProcessor firstProcessor;

    public enum States {
        CONNECTING,CONNECTED,ERROR, SHUTDOWN,RUNNING;
    }

    public void startup() {
        setupRequestProcessor();
        setStatus(States.RUNNING);
    }

    private void setStatus(States status) {
        this.states = status;
    }

    private void setupRequestProcessor() {
        FinalRequestProcessor finalRequestProcessor = new FinalRequestProcessor(this,null);
        SyncRequestProcessor syncRequestProcessor = new SyncRequestProcessor(this,finalRequestProcessor);
        syncRequestProcessor.start();
        this.firstProcessor = new PreRequestProcessor(this,syncRequestProcessor);
        ((PreRequestProcessor)this.firstProcessor).start();
    }

    public void startdata() {

    }

    public void registerNioServerCnxnFactory(NioServerCnxnFactory nioServerCnxnFactory) {
        this.nioServerCnxnFactory = nioServerCnxnFactory;
    }

    public void registerZkServerShutdownHandler(ZkServerShutdownHandler zkServerShutdownHandler) {
        this.zkServerShutdownHandler = zkServerShutdownHandler;
    }

}